
package org.sopt.dev.ImgKnight.monitor.chart;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.sopt.dev.ImgKnight.monitor.Monitor;
import org.sopt.dev.ImgKnight.monitor.nio.Peer;
import org.sopt.dev.ImgKnight.monitor.operation.Operations;


public class ChartDataGenerator {

	private Monitor monitor = null;
	
	private int inteval = 0;
	
	private Timer requestGenerator = null;
	
	public ChartDataGenerator(Monitor monitor, int inteval) {
		this.monitor = monitor;
		this.inteval = inteval;
	}
	
	public void open() {
		setDataGeneratRatio(inteval);
	}
	
	public void setDataGeneratRatio(int millisec) {
		if( requestGenerator != null )
			requestGenerator.cancel();
		requestGenerator = new Timer();
		requestGenerator.scheduleAtFixedRate((new TimerTask() {
			public void run() {
				Peer frontierPeer = monitor.getFrontierPeer();
				Peer[] agentPeers = monitor.getAgentPeers();
				if( frontierPeer != null ) {
					Operations.infoCondition(frontierPeer.getChannel());
					Operations.infoAgentTrafic(frontierPeer.getChannel());
				}
				if( agentPeers != null ) {
					for(Peer agent : agentPeers) {
						if( agent.isConnected() ) {
							Operations.infoCondition(agent.getChannel());
							Operations.infoTrafic(agent.getChannel());
						}
					}
				}				
			}
		}), new Date(), millisec);
	}
	
	public void close() {
		requestGenerator.cancel();
	}
	
}
